require "BagTableLayer"
require "HeroEquipLayer"
require "Equip"

ZBCDLayer = {}
ZBCDLayerFunc = {}
ZBCDLayerFunc.__index = ZBCDLayerFunc
setmetatable(ZBCDLayer, ZBCDLayerFunc)
ZBCDLayer.NodeID = {Menu=1001, TableBK=1002}

ZBCDLayerFunc.getNode = function()
  local l = {}
  setmetatable(l, ZBCDLayerFunc)
  l.node = CCBReader:load("ZhuangBeiLayer3.ccbi")
  ZBCDLayer.instance = l
  l:init()
  return l
end
ZBCDLayerFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZBCDLayer:getNode().node)
  --print("ZBCDLayer.getScene", sc)
  return sc
end

ZBCDLayerFunc.init = function(self)
  --print("ZBCDLayerFunc.init node", self.node, menu)
  local tb = self.node:getChildByTag(ZBCDLayer.NodeID.TableBK)
  self.bagTable = BagTable:create(TypeCast(tb, "CCSprite"), self, "onClickBagEquip", 1) 
  self.node:addChild(self.bagTable.node)

  self.heroEquipLayer = HeroEquipLayer:create(self, "onClickHeroEquip") 
  self.node:addChild(self.heroEquipLayer.node)
  setTouchEnabledAll(self.bagTable.node, true) 
  
  --ZhuangBeiGuide.nextStep()
end

ZBCDLayerFunc.onClickHeroEquip = function(self, node, event)
  self:setEnableLayer(false)
  self.selectedType = 1
  self.currentSelectedEquipIndex = node:getTag()
  local equip = Equip:create(node:getTag())
  self.tipLayer = equip:createTip(self)
  self.node:addChild(self.tipLayer)
  --setTouchEnabledAll(self.bagTable.node, false) 
  --setTouchEnabledAll(self.heroEquipLayer.node, false)
end
ZBCDLayerFunc.onClickBagEquip = function(self, node, event)
  self:setEnableLayer(false)
  self.currentSelectedEquipIndex = nil
  self.currentSelectedItemIndex = nil
  if node:getTag()<100000 then
    self.selectedType = 0
    self.currentSelectedEquipIndex = node:getTag()
    local equip = Equip:create(node:getTag())
    self.tipLayer = equip:createTip(self)
    self.node:addChild(self.tipLayer)
    --setTouchEnabledAll(self.bagTable.node, false) 
    --setTouchEnabledAll(self.heroEquipLayer.node, false)
    ZhuangBeiGuide.nextStep()
  else
    local index = node:getTag()-100000
    self.currentSelectedItemIndex = index 
    local item = Item:createWithIndex(index)
    self.tipLayer = item:createTip(self)
    self.node:addChild(self.tipLayer)
    --setTouchEnabledAll(self.bagTable.node, false) 
    --setTouchEnabledAll(self.heroEquipLayer.node, false)
  end
end

ZBCDLayerFunc.onEquipComplete = function(self, data)
  --self.node:removeChild(self.tipLayer, true)
  --setTouchEnabledAll(self.bagTable.node, true) 
  --setTouchEnabledAll(self.heroEquipLayer.node, true)
  LoadScene:removeLoadingScene()
  self:setEnableLayer(true)
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  --print("ZBCDLayerFunc.onEquipComplete", string.len(data))
  local res = cjson.decode(data)
  if res.ret==0 and res.data.result==true then
    NetworkRequest.syncData(res)
    self.bagTable:updateEquipsExt()
    self.heroEquipLayer:showEquip()
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
  ZhuangBeiGuide.nextStep()
end
ZBCDLayerFunc.onSellComplete = function(self, data)
  --self.node:removeChild(self.tipLayer, true)
  --setTouchEnabledAll(self.bagTable.node, true) 
  --setTouchEnabledAll(self.heroEquipLayer.node, true)
  --print("ZBCDLayerFunc.onSellComplete", data)
  LoadScene:removeLoadingScene()
  self:setEnableLayer(true)
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  local res = cjson.decode(data)
  if res.ret==0 and res.data.result==true then
    NetworkRequest.syncData(res)
    self.bagTable:updateEquipsExt()
    showMessage(string.format("cash:%d", res.data.sellCash), ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end

ZBCDLayerFunc.setEnableLayer = function(self, enable)
  print("ZBCDLayerFunc.setEnableLayer", enable)
  --self.bagTable:setEnable(enable)
  --self.heroEquipLayer:setEnable(enable)
  --setTouchEnabledAll(self.heroEquipLayer.node, enalbe)
  --setTouchEnabledAll(self.bagTable.node, enalbe)
  setTouchEnabledAll(ZhuangBeiScene.instance.node, enable)
end

ZBCDLayerFunc.onCloseTip = function(self, node)
  self:setEnableLayer(true)
  --setTouchEnabledAll(self.bagTable.node, true) 
  --setTouchEnabledAll(self.heroEquipLayer.node, true)
  node:getParent():getParent():removeChild(node:getParent(), true)
end

ZBCDLayerFunc.onSellButton = function(self, node)
  if self.currentSelectedEquipIndex~=nil then
    local equipIDs = UserData.userinfo.host.equips
    NetworkRequest.batchSell(equipIDs[tostring(self.currentSelectedEquipIndex)].p, "onSellComplete", self)
  elseif self.currentSelectedItemIndex~=nil then
    NetworkRequest.batchSell(self.currentSelectedItemIndex, "onSellComplete", self)
  end
  --node:getParent():getParent():removeChild(node:getParent(), true)
  self.node:removeChild(self.tipLayer, true)
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  self:setEnableLayer(false)
end
ZBCDLayerFunc.onEquipButton = function(self, node)
  --print("ZBCDLayerFunc.onEquipButton", self, self.heroEquipLayer.currentHeroID, self.currentSelectedEquipIndex)
  if self.selectedType==1 then
    NetworkRequest.takeOff(self.heroEquipLayer.currentHeroID, self.currentSelectedEquipIndex, "onEquipComplete", self)
  else
    NetworkRequest.equip(self.heroEquipLayer.currentHeroID, self.currentSelectedEquipIndex, "onEquipComplete", self)
  end
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  self:setEnableLayer(false)
  self.node:removeChild(self.tipLayer, true)
  LoadScene:showLoadingScene()
  --node:getParent():getParent():removeChild(node:getParent(), true)
end
ZBCDLayerFunc.onUseButton = function(self, node)
  print("ZBCDLayerFunc.onUseButton", self.currentSelectedItemIndex)
  self:setEnableLayer(false)
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  self.node:removeChild(self.tipLayer, true)
  LoadScene:showLoadingScene()
  NetworkRequest.useItem(self.currentSelectedItemIndex, "onUseItemComplete", self)
end
ZBCDLayerFunc.onUseItemComplete = function(self, data)
  print("ZBCDLayerFunc.onUseItemComplete", data)
  LoadScene:removeLoadingScene()
  self:setEnableLayer(true)
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  local res = cjson.decode(data)
  if res.ret==0 and res.data.result==true then
    NetworkRequest.syncData(res)
    self.bagTable:updateEquipsExt()
    showMessage("物品使用成功", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end


